/******************************************************************************
 * Include files
 ******************************************************************************/
#include "..\inc\main.h"
#include "cw32l010_systick.h"
#include "cw32l010_sysctrl.h"
#include "cw32l010_gpio.h"
#include "cw32l010_adc.h"
#include "FM17550.h"
#include "task.h"
#include "readAPI.h"
#include "dev.h"



void TEST(){

   //UartGetReg(JREG_VERSION,&reg_data);
  // GetReg(JREG_VERSION,&reg_data); 

}



int main(void)
{
	unsigned char polling_card;
	unsigned char reg_data,m;
	//Device_Init(); 
	// RCC_Configuration();
	 //GPIO_Configuration();
	// SPI_Configuration();

	//UART配置


	Uart_RCC_Configuration();
	Uart_GPIO_Configuration();
	UART_Configuration1();

	

	LED_ON
	

	FM175XX_HardReset();	//FM hardware 复位

for (int i = 0; i < 3; i++)
{
  UartGetReg(JREG_VERSION,&reg_data);
    
}

  
  if((reg_data == 0xa1) || (reg_data == 0xa2)){
    UartSetReg(JREG_SERIALSPEED,0x7a);
    mDelay(10);
    UART_Configuration();
    reg_data=0x00;
    UartGetReg(JREG_VERSION,&reg_data);
	  UartGetReg(JREG_VERSION,&reg_data);
  }

	LED_OFF


	while (1)
	{

    SetCW(TX1_TX2_CW_ENABLE);	//TX1 TX2 载波

		// SysTickDelay(6);

		// TEST();
		// if(reg_data == 0xa1){
		// 	TYPE_A_EVENT();
		// }
	if(FM175XX_Polling(&polling_card)== SUCCESS)
	{							
		if(polling_card & BIT0)//TYPE A
		{	
			TYPE_A_EVENT();		
			LED_ON		
		}

		if(polling_card & BIT1)
		{							
			TYPE_B_EVENT();
			LED_ON
		}
	
	}

    SetCW(TX1_TX2_CW_DISABLE);
    mDelay(1000);


	}
     
}


void mDelay(unsigned int ms)			//ʵ��1ms
{
	unsigned int i;
	unsigned int j;
	for(j=0;j<ms;j++)
	{
		for(i=0;i<800;i++)
			;
	}
}


//�¼�A
void TYPE_A_EVENT(void)
{
  unsigned char result;
  unsigned char *uid;

  SetCW(TX1_TX2_CW_DISABLE);
  FM175XX_Initial_ReaderA();//初始化读卡器A
  SetCW(TX1_TX2_CW_ENABLE);
  result = ReaderA_CardActivate();
  if(result != FM175XX_SUCCESS)	//通信失败关闭载波
  {
    SetCW(TX1_TX2_CW_DISABLE);		
    return;
  }

LED_ON

  
  // LED_OFF;
  // FM175XX_HardReset();

  // SetCW(TX1_TX2_CW_ENABLE);	//TX1 TX2 载波	
  // FM175XX_Initial_ReaderA();//初始化读卡器A
  
  // result = ReaderA_CardActivate();
	
  // if(result != FM175XX_SUCCESS)	//通信失败关闭载波
  // {
  //   SetCW(TX1_TX2_CW_DISABLE);		
  //   return;
  // }
  // LED_ON;
  // uid = PICC_A.UID;
  
  // SetCW(TX1_TX2_CW_DISABLE);	
  // return;
} 


void TYPE_B_EVENT(){

}



/******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @return None
  */
void assert_failed(uint8_t* file, uint32_t line)  
{
    /* USER CODE BEGIN 6 */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END 6 */
}


#endif /* USE_FULL_ASSERT */

